<?php
set_time_limit(30);error_reporting(0);
ini_set('memory_limit', '6G');
		/**
			排除一下后缀不写入统计
		**/
		$request_uri = $_SERVER['REQUEST_URI'];
		// 使用正则表达式检查请求是否为静态资源文件
		if (preg_match('/\.(css|js|png|jpg|jpeg|gif|ico)$/i', $request_uri)) {
			// 如果文件不存在，则返回 404 错误
			if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $request_uri)) {
				//asd();
				exit();
			}
		}

		//global $agent;
        header('HTTP/1.1 200 OK');
		header('status: 200 OK');
		header('content-type:text/html;charset=utf-8');
		$canshu='/tongji/'.$_SERVER['QUERY_STRING'].'/';
		$hour_path=str_replace('\\','/',__DIR__).'/tongji/hour/';
		$cache_path = str_replace('\\','/',__DIR__).$canshu;
        $cache_path1 = str_replace('\\','/',__DIR__).'/tongji/Sogou/';
		$cache_path2 = str_replace('\\','/',__DIR__).'/tongji/Baiduspider/';
		$cache_path3 = str_replace('\\','/',__DIR__).'/tongji/360Spider/';
		$cache_path4 = str_replace('\\','/',__DIR__).'/tongji/Googlebot/';
		$cache_path5 = str_replace('\\','/',__DIR__).'/tongji/Yisouspider/';
		$cache_path6 = str_replace('\\','/',__DIR__).'/tongji/Bytespider/';
        $cache_path7 = str_replace('\\','/',__DIR__).'/tongji/bingbot/';
        $key= $_SERVER['HTTP_USER_AGENT'];

        $Sogouspider =preg_match('/Sogou/i', $key, $Sogouspider);
        $baiduspider =preg_match('/Baiduspider/i', $key, $baiduspider);
        $Googlebot =preg_match('/Googlebot/i', $key, $Googlebot);
        $bingbot =preg_match('/bingbot/i', $key, $bingbot);
		$liulingSpider=preg_match('/360Spider/i',$key, $liulingSpider);
		$Yisouspider=preg_match('/YisouSpider/i',$key, $Yisouspider);
		$Bytespider=preg_match('/Bytespider/i', $key, $Bytespider);
		//var_dump($_SERVER);
		$ip = $_SERVER['REMOTE_ADDR'];
		$urls=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		
		define('wapp','@iPhone|iPod|iPad|Android|ios@i');
		if(stristr($key,'Baiduspider-render')){
			$baidu='百度渲染';
		}elseif(preg_match(wapp,$key) && stristr($key,'Baiduspider')){
			$baidu='百度移动';
		}elseif(stristr($key,'Baiduspider')){
			$baidu='百度pc';
		}

		if(preg_match(wapp,$key) && stristr($key,'sogou wap') || preg_match(wapp,$key) &&stristr($key,'SogouMobile') || preg_match(wapp,$key) && stristr($key,'SogouMSE')){
			$sogou='搜狗移动';
		}elseif(stristr($key,'Sogou web')){
			$sogou='搜狗pc';
		}else{
			$sogou='搜狗移动';
		}

		if(preg_match(wapp,$key) && stristr($key,'360Spider')){
			$liuling='360移动';
		}elseif(stristr($key,'360Spider')){
			$liuling='360pc';
		}

		if(preg_match(wapp,$key) && stristr($key,'Yisouspider')){
			$yisou='神马移动';
		}elseif(stristr($key,'Yisouspider')){
			$yisou='神马pc';
		}
		
		if(preg_match(wapp,$key) && stristr($key,'ToutiaoSpider') || preg_match(wapp,$key) && stristr($key,'Bytespider') || preg_match(wapp,$key) && stristr($key,'ByteDanceSpider')){
			$toutiao='今日头条移动';
		}elseif(stristr($key,'ToutiaoSpider') || stristr($key,'Bytespider') || stristr($key,'ByteDanceSpider')){
			$toutiao='今日头条pc';
		}
		
		if(preg_match(wapp,$key) && stristr($key,'Googlebot')){
			$google='谷歌移动';
		}elseif(stristr($key,'Googlebot')){
			$google='谷歌pc';
		}
        if(preg_match(wapp,$key) && stristr($key,'bingbot')){
            $bing='必应移动';
        }elseif(stristr($key,'bingbot')){
            $bing='必应pc';
        }
		$zhizhutime=date('Y-m-d H:i:s' );
		$zhizhutimes=date('Ymd');
		$hour=date('H');
		$gouzi="$zhizhutime--$ip--$sogou--$urls\n"; //Sogou
		$baidus="$zhizhutime--$ip--$baidu--$urls\n"; //百度
		$googles="$zhizhutime--$ip--$google--$urls\n";//谷歌
		$liulingSpiders="$zhizhutime--$ip--$liuling--$urls\n"; //360
		$Yisouspiders="$zhizhutime--$ip--$yisou--$urls\n"; //神马
		$Bytespiders="$zhizhutime--$ip--$toutiao--$urls\n"; //头条
        $bings="$zhizhutime--$ip--$bing--$urls\n"; //必应
		$dir = $hour_path.$zhizhutimes;
        is_dir($dir)?:mkdir($dir,0777,true);
		if($Sogouspider){
            file_put_contents($cache_path1."$zhizhutimes.log",$gouzi,FILE_APPEND);
            file_put_contents($hour_path.$zhizhutimes.'/'.$hour.".log",'1',FILE_APPEND);
		} 
		if($baiduspider){
            file_put_contents($cache_path2."$zhizhutimes.log",$baidus,FILE_APPEND);
            file_put_contents($hour_path.$zhizhutimes.'/'.$hour.".log",'1',FILE_APPEND);
		}
		if($Googlebot){
            file_put_contents($cache_path4."$zhizhutimes.log",$googles,FILE_APPEND);
            file_put_contents($hour_path.$zhizhutimes.'/'.$hour.".log",'1',FILE_APPEND);
		}
        if($liulingSpider){
            file_put_contents($cache_path3."$zhizhutimes.log",$liulingSpiders,FILE_APPEND);
            file_put_contents($hour_path.$zhizhutimes.'/'.$hour.".log",'1',FILE_APPEND);
		}
		if($Yisouspider){
            file_put_contents($cache_path5."$zhizhutimes.log",$Yisouspiders,FILE_APPEND);
            file_put_contents($hour_path.$zhizhutimes.'/'.$hour.".log",'1',FILE_APPEND);
		}
		if($Bytespider){
            file_put_contents($cache_path6."$zhizhutimes.log",$Bytespiders,FILE_APPEND);
            file_put_contents($hour_path.$zhizhutimes.'/'.$hour.".log",'1',FILE_APPEND);
		}
        if($bingbot){
            file_put_contents($cache_path7."$zhizhutimes.log",$bings,FILE_APPEND);
            file_put_contents($hour_path.$zhizhutimes.'/'.$hour.".log",'1',FILE_APPEND);
        }
?>